package com.cus.tom.transfer;


import static com.cus.tom.transfer.bean.Const.CHANNEL_TRANSFER_RESULT;
import static com.cus.tom.transfer.bean.Const.CHANNEL_TRANSFER_STATE;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Build;

import com.cus.tom.transfer.kt.MyApp;
import com.cus.tom.transfer.trans.io.AppReceiver;
import com.cus.tom.transfer.trans.service.ReceiveService;
import com.cus.tom.transfer.trans.service.SendService;

public class App extends MyApp {

    static App app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addDataScheme("package");
        registerReceiver(new AppReceiver(), filter);
    }

    public static App getApp() {
        return app;
    }

    private ReceiveService receiveService = null;
    public SendService sendService = null;

    public void receiveStart(ReceiveService service) {
        receiveService = service;
    }

    public void receiveEnd(boolean success) {
        receiveService = null;
    }

    public void createNotificationChannels(NotificationManager manager, Resources res) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            {
                NotificationChannel channel = new NotificationChannel(CHANNEL_TRANSFER_STATE,
                        res.getString(R.string.channel_transfer_state), NotificationManager.IMPORTANCE_DEFAULT);
                channel.enableLights(false);
                channel.enableVibration(false);
                channel.setSound(null, null);

                manager.createNotificationChannel(channel);
            }
            {
                NotificationChannel channel = new NotificationChannel(CHANNEL_TRANSFER_RESULT,
                        res.getString(R.string.channel_transfer_result), NotificationManager.IMPORTANCE_DEFAULT);
                channel.setSound(null, null);

                manager.createNotificationChannel(channel);
            }
        }
    }
}
